﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;

namespace Billing.Win
{
    [ToolboxItem(true)]
    public partial class ucClientView : Billing.WindowsBase.CustomControls.BaseControl
    {
        private Type elementViewType = typeof(Infragistics.Win.Misc.UltraLabel);

        public Type ElementViewType
        {
            get { return elementViewType; }
            set { elementViewType = value; }
        }

        private object dataObject;

        public object DataObject
        {
            get { return dataObject; }
            set
            {
                dataObject = value;
                SetDataObject(dataObject);
            }
        }

        private void SetDataObject(object dtObj)
        {
            if (dtObj == null)
            {
                DoClearView();
                return;
            }
            Type objTp = dtObj.GetType();
            foreach (Control c in this.Controls)
            {
                if (c.GetType().Equals(elementViewType))
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(c.Tag)))
                        c.Text = GetTextValueFromObject(Convert.ToString(c.Tag), dtObj, objTp);
                }
            }
        }

        private string GetTextValueFromObject(string propertyName, object dtObj, Type objTp)
        {
            if (dtObj == null) return string.Empty;

            foreach (System.Reflection.PropertyInfo pInf in objTp.GetProperties())
            {
                if (pInf.Name.Equals(propertyName))
                {
                    if (pInf.PropertyType.Equals(typeof(bool)))
                    {
                        return Convert.ToBoolean(pInf.GetValue(dtObj, null)) ? "დიახ" : "არა";
                    }
                    return Convert.ToString(pInf.GetValue(dtObj, null));
                }
            }
            return string.Empty;
        }

        private void DoClearView()
        {
            foreach (Control c in this.Controls)
            {
                if (c.GetType().Equals(elementViewType))
                {
                    if (!string.IsNullOrEmpty(Convert.ToString(c.Tag)))
                        c.Text = string.Empty;
                }
            }
        }

        public ucClientView()
        {
            InitializeComponent();
        }
    }
}
